//
//  LoginViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/12.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "HUser.h"

@interface LoginViewController (){
    
}
@property (nonatomic, strong) UITextField *usernames;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UIButton *loginOfButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.centerLabel.text = @"登陆注册";
    self.view.backgroundColor = [UIColor colorWithWhite:0.906 alpha:1.000];
    [self.leftButton setImage:[UIImage imageNamed:@"个人中心-left@2x"] forState:UIControlStateNormal];
    [self.rightButton setTitle:@"注册" forState:UIControlStateNormal];
    
    [self initUi];
    [self initData];
    
}

-(void)initUi{
    [self.view addSubview:self.usernames];
    [self.view addSubview:self.password];
    [self.view addSubview:self.loginOfButton];
}

-(void)initData{
    
    
}

// 隐藏下面的分栏标签，进入这个界面的时候隐藏，离开界面的时候显示
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}


#pragma mark -- 点击事件

-(void)loginOfButton_action{
    __block BOOL islogin = NO;
    [[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self.usernames.text isEqualToString:obj[@"username"]] && [self.password.text isEqualToString:obj[@"password"]]) {
            // 登录成功
            if (self.block) {
                self.block(obj);
            }
            [HUser currentUser].isLogin = YES;
            [HUser currentUser].name = obj[@"nicheng"];
            islogin = YES;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });

        }
    }];
    if (!islogin) {
      
            //登录失败
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已存在或输入错误" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            [alertController addAction:photoAction];
            [self presentViewController:alertController animated:YES completion:nil];
       
    }
    
    }

// 继承父类的点击事件
-(void)action_RightButtonClick:(UIButton *)sender{
    RegisterViewController *vc = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - getter
- (UITextField *)usernames{
    if (!_usernames) {
        _usernames = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.15, kwidth * 0.95, khieght * 0.07)];
        _usernames.placeholder = @"帐号";
        _usernames.borderStyle = UITextBorderStyleRoundedRect;
        _usernames.clearButtonMode = UITextFieldViewModeWhileEditing;
        _usernames.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        image.image = [UIImage imageNamed:@"zhanghu"];
        _usernames.leftView = image;
    }
    return _usernames;
}

- (UITextField *)password{
    if (!_password) {
        _password = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.23, kwidth * 0.95, khieght * 0.07)];
        _password.placeholder = @"请输入密码";
        _password.secureTextEntry = YES;
        _password.borderStyle = UITextBorderStyleRoundedRect;
        _password.clearButtonMode = UITextFieldViewModeWhileEditing;
        _password.secureTextEntry = YES;
        // uitextfield 左侧图像在什么时候存在
        _password.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth * 0.1, khieght * 0.05)];
        imageView.image = [UIImage imageNamed:@"mima"];
        _password.leftView = imageView;
    }
    return _password;
}
- (UIButton *)loginOfButton{
    if (!_loginOfButton) {
        _loginOfButton = [[UIButton alloc] initWithFrame:CGRectMake(kwidth * 0.015, khieght * 0.35, kwidth * 0.95, khieght * 0.07)];
        _loginOfButton.layer.cornerRadius = 8;
        _loginOfButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.467 blue:0.949 alpha:1.000];
        [_loginOfButton setTitle:@"登陆" forState:UIControlStateNormal];
        [_loginOfButton addTarget:self action:@selector(loginOfButton_action) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginOfButton;
}

@end
